package com.monkeyk.sos.web.controller.sys.cooperatormgt;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.monkeyk.sos.domain.model.CooperDetails;
import com.monkeyk.sos.service.CooperService;
import com.monkeyk.sos.util.PageData;
import com.monkeyk.sos.util.Pager;
import com.monkeyk.sos.web.controller.BaseController;


//合作者管理
@Controller
@RequestMapping("/sys/cooperatormgt")
public class CooperatorMgtController extends BaseController {
	private static final Logger	Log	= Logger.getLogger(CooperatorMgtController.class);
	
	@Autowired
	private CooperService		cooperService;
	

	/**
	 * 合作者详情
	 * @param request
	 * @param response
	 * @param rows
	 * @param page
	 */
	@RequestMapping(value="/cooperatordetail",method= {RequestMethod.POST , RequestMethod.GET})
	@ResponseBody
	public void getCooperatorDetail(HttpServletRequest request ,HttpServletResponse response,
		                      @RequestParam int rows,@RequestParam int page) {
	    
	    Log.info("合作者详情: " + "/sys/cooperatormgt/cooperatordetail");
	    
	    List<CooperDetails> cooperDetailList  = new ArrayList<CooperDetails>();
	    //去后台查询所需参数 pageInfo cooperDetails
	    Pager pageInfo = new Pager(page, rows);
	    CooperDetails cooperDetails = new CooperDetails();
	    cooperDetailList = cooperService.getSelectedCooperDetailsList(cooperDetails, pageInfo);
	    int totalResults = cooperService.getAllCooperDetailsTotals();
	    //返回页面数据
	    PageData<CooperDetails> pageData = new PageData<CooperDetails>();
	    pageData.setRows(cooperDetailList);
	    pageData.setTotal(totalResults);
	    pageInfo.setData(cooperDetailList);
	    pageInfo.setTotalResults(totalResults);
	    writeJSON(response, pageData);
	    
	}
	/**
	 * 软删除 所选择的用户信息
	 * @param request
	 * @param response
	 * @param cooperIDs
	 * @param status
	 * @return String
	 */
	@RequestMapping(value="/delcooperatordetail",method=RequestMethod.POST)
	@ResponseBody
	public int   delcooperatordetail(HttpServletRequest request,HttpServletResponse response,
		                           @RequestParam("st") int status,@RequestParam("id") String cooperids) {
	    String[] ids= cooperids.split(",");
	    for (int i = 0; i < ids.length; i++) {
		cooperService.updateStatus(2,ids[i]);
	    }
	    return 1;
	}
	
	
	
	/*下面是跳转页面*/
	
	/*
	 * 管理员首页
	 */
	@RequestMapping("/mgtindex")
	public String getMgtIndex() {
	    Log.info("管理员首页 : " + "/sys/cooperatormgt/mgtindex");
	    return "sys/sysIndex";
	}
	
	/*
	 * 合作者管理
	 */
	@RequestMapping("/cooperator_mgt")
	public String cooperatorMgt() {
	    Log.info("合作者管理 : " + "/sys/cooperatormgr/cooperator_mgt");
	    return "sys/cooperatormgt/cooperator_mgt";
	}
	
	

	
}
